package chixing.day20.class23;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 *1,创建代理对象
 * 2 增强实现，在商品进行增删改查后，添加日志
 */
public class MyProxy implements InvocationHandler {
    private Object target; //new ProductServiceImpl()
    public MyProxy(Object target) {
        this.target = target;
    }
    //1 创建代理对象
    public Object creativeProxy() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);

    }
    //2 增强实现，在商品进行增删改查后，添加日志
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
        System.out.println("目标对象正在调用"+method.getName());
        proxy =this.target;
        Object returnValue = method.invoke(proxy, args);
        if(method.getName().equals("add")||method.getName().equals("update")||method.getName().equals("delete")){
            LogUtil logUtil = new LogUtil();
            logUtil.addLog();
        }
        return returnValue;
    }

}
//public class MyProxy implements InvocationHandler {
// private Object target;
// public MyProxy(Object target) {
//  this.target = target;
// }
// public Object creativeProxy() {
//   return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
//}
// public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable{
//  System.out.println("目标对象正在调用"+method.getName());
//  proxy =this.target;
//  Object returnValue = method.invoke(proxy, args);
//  return returnValue;
// }
// }
// }